package com.chunfeng.service.impl;

import com.chunfeng.entity.Role;
import com.chunfeng.mapper.RoleMapper;
import com.chunfeng.result.JsonRequest;
import com.chunfeng.result.ServiceEnum;
import com.chunfeng.service.IRoleService;
import com.chunfeng.utils.SqlDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 角色业务层实现类
 *
 * @author by 春风能解释
 * <p>
 * 2022/12/20
 */
@Service
@Transactional
@Slf4j
public class RoleServiceImpl implements IRoleService {
    /**
     * 角色mapper注入
     */
    @Autowired
    private RoleMapper roleMapper;
    /**
     * 获取当前时间
     */
    private final String date = SqlDateUtils.date;

    /**
     * 查看所有角色信息
     *
     * @return JSON
     */
    @Override
    @Cacheable(value = "role")
    public JsonRequest<List<Role>> lookAllRoles() {
        List<Role> roles = roleMapper.selectAllRole();
        if (roles.isEmpty()) {
            log.warn("未查询到任何角色信息!");
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        log.info("已查询到{}条角色信息!", roles.size());
        return JsonRequest.success(roles);
    }

    /**
     * 按名称获取角色信息
     *
     * @param roleName 角色名
     * @return JSON
     */
    @Override
    @Cacheable(value = "role", key = "#roleName")
    public JsonRequest<List<Role>> lookRolesByName(String roleName) {
        List<Role> roles = roleMapper.selectRolesByName(roleName);
        if (roles.isEmpty()) {
            log.warn("未查询到任何角色信息!");
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        log.info("已查询到{}条角色信息!", roles.size());
        return JsonRequest.success(roles);
    }


    /**
     * 新增一条角色信息
     *
     * @param role 角色信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = "role", allEntries = true)
    public JsonRequest<Integer> insertOneRole(Role role) {
        role.setCreateUser(SqlDateUtils.currentUserId);
        role.setCreateTime(date);
        Integer column = roleMapper.insertRole(role);
        if (column < 1) {
            log.error("名为{}的角色信息添加失败!返回值:{}", role.getRoleName(), column);
            return JsonRequest.error(ServiceEnum.INSERT_ERROR);
        }
        log.info("名为{}的角色信息添加成功!返回值:{}", role.getRoleName(), column);
        return JsonRequest.success(column);
    }

    /**
     * 修改一条角色信息
     *
     * @param role 角色信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = "role", allEntries = true)
    public JsonRequest<Integer> updateOneRole(Role role) {
        Role role1 = roleMapper.selectAllRoleById(role.getId());
        if (Objects.isNull(role1)) {
            log.warn("未查询到ID为{}的角色信息!", role.getId());
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        //注入
        role.setUpdateUser(SqlDateUtils.currentUserId);
        role.setUpdateTime(date);
        Integer column = roleMapper.updateRoleById(role);
        if (column < 1) {
            log.error("ID为{}的角色信息修改失败!返回值:{}", role.getId(), column);
            return JsonRequest.error(ServiceEnum.UPDATE_ERROR);
        }
        log.info("ID为{}的角色信息修改成功!返回值:{}", role.getId(), column);
        return JsonRequest.success(column);
    }

    /**
     * 删除角色信息
     *
     * @param ids ID值
     * @return JSON
     */
    @Override
    @CacheEvict(value = "role", allEntries = true)
    public JsonRequest<Integer> deleteRole(Integer[] ids) {
        //判断角色信息是否完整
        Integer count = roleMapper.countRole(ids);
        if (count < ids.length) {
            log.warn("待删除的角色个数与数据库已存在的角色个数不符!数据库实际存在{}个,需要{}个", count, ids.length);
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        //删除逻辑
        Integer column = roleMapper.deleteRoleById(ids);
        if (column < 1) {
            log.error("删除角色遇到错误!返回值{}", column);
            return JsonRequest.error(ServiceEnum.DELETE_ERROR);
        }
        log.info("已删除{}条角色信息!返回值{}", count, column);
        return JsonRequest.success(column);
    }
}
